package com.bawei.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.bawei.base.RoutePath
import com.tencent.mmkv.MMKV

/**
 * Author: Jason Zhang
 * Date: 2024/8/21
 * Desc: 自定义路由拦截器
 */

@Interceptor(priority = 4, name = "路由Token拦截器")
class RouteTokenInterceptor: IInterceptor {

    private lateinit var mContext: Context

    //需要拦截下来Path
    private val needInterceptorPaths = arrayOf("")

    override fun init(context: Context?) {
        if (context != null){
            mContext = context
        }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        //通过判断path来决定是否需要拦截
        if (needInterceptorPaths.contains(postcard.path)){
            var token = MMKV.defaultMMKV().decodeString("token", "")
            if (token.isNullOrEmpty()){
                postcard.group = RoutePath.GROUP_LOGIN
                postcard.path = RoutePath.PATH_LOGIN_PAGE
                postcard.navigation()
            } else {
                callback.onContinue(postcard)
            }
        } else {
            //继续执行跳转逻辑
            callback.onContinue(postcard)
        }

    }





}